﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.TextCore.Text;
using UnityEngine.UIElements;

// [assembly: UxmlNamespacePrefix("Game.UI", "first")]
public class PlayerBaseInfo:VisualElement
{
    private Label ve_playerName => this.Q<Label>(className:"playerName");
    private VisualElement ve_headImg => this.Q<VisualElement>(className:"headImg");
    private Label ve_exp => this.Q<Label>(className:"exp");
    
    public Action headImgClick;
    public PlayerBaseInfo()
    {
        var asset = UnityEngine.Resources.Load<VisualTreeAsset>("UI/CumstomControl/PlayerBaseInfo/PlayerBaseInfo");
        asset.CloneTree(this);
        ve_headImg.RegisterCallback<ClickEvent>(e => headImgClick?.Invoke());
    }

    public new class UxmlFactory : UxmlFactory<PlayerBaseInfo,UxmlTraits> {}
    public new class UxmlTraits : VisualElement.UxmlTraits
    {
        UxmlStringAttributeDescription m_HeadImg = new UxmlStringAttributeDescription { name = "headImg" };
        UxmlStringAttributeDescription m_PlayerName = new UxmlStringAttributeDescription { name = "playerName" };
        UxmlStringAttributeDescription m_NowHp = new UxmlStringAttributeDescription { name = "nowHp" };
        UxmlStringAttributeDescription m_MaxHp = new UxmlStringAttributeDescription { name = "maxHp" };

        public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription
        {
            get { yield break; }
        }

        public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
        {
            base.Init(ve, bag, cc);
            ((PlayerBaseInfo)ve).headImg = m_HeadImg.GetValueFromBag(bag, cc);
            ((PlayerBaseInfo)ve).PlayerName = m_PlayerName.GetValueFromBag(bag, cc);
            ((PlayerBaseInfo)ve).nowHp = m_NowHp.GetValueFromBag(bag, cc);
            ((PlayerBaseInfo)ve).maxHp = m_MaxHp.GetValueFromBag(bag, cc);
        }
    }

    string headImg;
    string playerName;
    string nowHp;
    private string maxHp;
    
    public string HeadImg
    {
        get => headImg;
        set
        {
            headImg = value;
            Sprite spriteAsset = UnityEngine.Resources.Load<Sprite>(value);
            ve_headImg.style.backgroundImage = new StyleBackground(spriteAsset);
        }
    }
    public string PlayerName
    {
        get => playerName;
        set
        {
            playerName = value;
            ve_playerName.text = value;
        }
    }
    public string NowHp
    {
        get => nowHp;
        set
        {
            nowHp = value;
            ve_exp.text = nowHp+"/"+maxHp;
        }
    }
    public string MaxHp
    {
        get => maxHp;
        set
        {
            maxHp = value;
            ve_exp.text = nowHp+"/"+maxHp;
        }
    }
    
     
}